import time,json

import tornado.web
from util.myredis import r
from util.myjwt import myjwt
class  BaseHandler(tornado.web.RequestHandler):
    # 重写该方法
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*") # 允许所有的域名访问
        self.set_header("Access-Control-Allow-Headers", "*") # 允许携带所有的参数
        self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE") # 允许所有的请求方式
        self.set_header("Access-Control-Max-Age", "3600") # 允许连接时，最大响应时间
    # 定义一个响应的方法，不需要实现什么功能
    def options(self):
        pass

    # def prepare(self):
    #     white = ['/login']
    #     url = self.request.uri
    #     if url not in white:
    #          # 获取token
    #         try:
    #             headers = self.request.headers
    #             token = headers['token']
    #             flag = myjwt.check(token)
    #             if flag == True:
    #                 #解析验证时间
    #                 payload = myjwt.jwt_decode(token)
    #                 ptime = payload['data']['exp']
    #                 now = time.time()
    #                 if int(now)-int(ptime)>3600:
    #                     self.finish({"code":402,"msg":"token已过期"})
    #
    #                 key = 'logoutlist'
    #                 res = r.sortedset_getall(key)
    #                 print('*******************',res)
    #                 for i in res:
    #                     if token == i.decode():
    #                         self.finish({'code': 300,'msg':'清空'})
    #                 key = 'gettype' + str(payload['data']['roleid'])
    #                 # 判断此token是否退出
    #
    #
    #                 urls = r.str_get(key)
    #                 if url not in json.loads(urls):
    #                     self.finish({'code':408,"msg":"无接口权限"})
    #                 #接口权限验证
    #                 #  {"data": {"name": "zs", "userid": 1,'exp':}}
    #         except:
    #                 self.finish({"code": 401, "msg": "token已被修改"})



